package com.lab.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;



/**
 * @author WuHao
 * @since Jul 28, 2011 3:01:54 PM
 * @version 2011
 */
public class SocketMethod {

	/**
	 * cacheCode加分隔符
	 */
	public static String addSpliter(String inputString){
		String temp ="";
		for(int i =0;i<(inputString.length()-2);i+=2){
			temp+=(inputString.substring(i,i+2)+"!");
			if(i>=(inputString.length()-4)){
				temp+=inputString.substring(i+2,inputString.length());
			}
		}
		return temp;
	}
	/**
	 * 对输入值进行CRC验证
	 * @param inputValueString
	 * @return Byte[]
	 */
	public static String StringCrc(String inputValueString)
	{
		byte[] sbuf = CRC16M.getSendBuf(inputValueString);
		String resultString = CRC16M.getBufHexStr(sbuf);
		return resultString;
	}
	
	/**
	 * 对输入值处理成Byte[]
	 * @param inputValueString
	 * @return Byte[]
	 */
	public static byte[] String2byteArray(String inputValueString)
	{
		String[] resultArray = inputValueString.split("!");//临时String数组
		byte[] rsByteArray = new byte[resultArray.length];//转换目标 的Byte数组
		for(int i = 0; i< resultArray.length  ; i ++)
		{
			rsByteArray[i] = CRC16M.getSuperNBFunc(resultArray[i]);
		}
		return rsByteArray;
	}
	
	/**
	 * 获取计算公式需要的Byte[]数组
	 * @param iss
	 * @return byte[]
	 */
	public static byte[] getCalByteArray(InputStream iss)
	{
		int last = 0; //判断阻塞长度
		int arrayCount = 0; //数组计数器
		byte[] calByteArray = null;
		int flag;
		try {
			flag = iss.read();
			last = iss.available();
			String[] result = new String[last+1];
			while (flag != -1) {
				result[arrayCount] = UtilTools.addZeroForNum(UtilTools.mkHexStr(flag),2);
				arrayCount++;
				last = iss.available();
				flag = (last==0)?-1:iss.read();
			}
			
			calByteArray = new byte[result.length];//转换目标 的Byte数组
			for(int i = 0; i< result.length  ; i ++)
			{
				calByteArray[i] = CRC16M.getSuperNBFunc(result[i]);
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		return calByteArray;
	}
	
	
	public static String mkSocket(String socketIp,String socketPort,String socketCode) throws IOException{
		String returnCode = "";
		InputStream iss = null;
		OutputStream oss = null;
		Socket socket = null;
		try {
			System.out.println("IP:" + socketIp);
			System.out.println("Port:" + socketPort);
			System.out.println("Code:" + socketCode);
			socket = new Socket(socketIp, Integer.parseInt(socketPort));
			socket.setSoTimeout(1000);
			iss = socket.getInputStream();
			oss = socket.getOutputStream();
			System.out.println("input String==" + socketCode);
			String[] resultArray = socketCode.split("!");// 临时String数组
			byte[] bSend = new byte[resultArray.length];// 转换目标 的Byte数组
			for (int i = 0; i < resultArray.length; i++) {
				bSend[i] = CRC16M.getSuperNBFunc(resultArray[i]);
			}
			oss.write(bSend);
			oss.flush();
			int last = 0; // 判断阻塞长度
			int flag;
			flag = iss.read();
			last = iss.available();
			while (flag != -1) {
				returnCode += UtilTools.addZeroForNum(UtilTools.mkHexStr(flag), 2) + " ";
				last = iss.available();
				flag = (last == 0) ? -1 : iss.read();
			}
			System.out.println("returnCode==" + returnCode);
		} catch (Exception e) {
			returnCode = "异常："+e.getMessage();
			e.printStackTrace();
		}finally{
			if(null!=oss)oss.close();
			if(null!=iss)iss.close();
			if(null!=socket)socket.close();
		}
		return returnCode;
	}
	
}
